package br.usp.icmc.biomac.client.view.impl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


public class LoginForm extends DialogBox  {
	
	
	private static LoginFormUiBinder uiBinder = GWT
			.create(LoginFormUiBinder.class);

	interface LoginFormUiBinder extends UiBinder<Widget, LoginForm> {
	}

	@UiField
	  TextBox username;
	@UiField
	  PasswordTextBox password;
	@UiField
	  Button okButton;
	@UiField
	  Button cancelButton;

	/**
     * Instantiates new login dialog.
     *
     */
	public LoginForm() {
		setHTML("<div style='text-align:center'>Sign In to your ePAD Account</div>");
		setWidget(uiBinder.createAndBindUi(this));
		
		// Enable animation.
	    setAnimationEnabled(true);
	    
	    // Enable glass background.
	    setGlassEnabled(true);
	    
	    // Make it modal and enable auto-hide
	    setModal(true);
	    setAutoHideEnabled(true);
	    
	    // set the id an styles
	    this.getElement().setId("loginForm");
	    okButton.setStyleName("btn-primary");
		cancelButton.setStyleName("btn-primary");

	}
	
		
	// Using the DOM element, slide down the div
		public static native void slideDown(final com.google.gwt.dom.client.Element element) /*-{
		        //$wnd.jQuery(element).hide();
		        $wnd.jQuery(element).slideDown(500);
		}-*/;

	// Using the DOM element, slide up the div
	    public static native void slideUp(final com.google.gwt.dom.client.Element element) /*-{
			    $wnd.jQuery(element).slideUp(1);
		}-*/;


	  @UiHandler("okButton")
	  void onClick(
	    ClickEvent e){
		  
		  if (username.getText().equals("guest") && password.getText().equals("guest")) {
			  // sign the user in
			  LoginForm.this.hide();
		  }
	 
		  else {
			  Element el = DOM.getElementById("login-error-message");
			  slideDown(el);			  
		  }
		  
	  }
	  
	  @UiHandler("cancelButton")
	  void onCancel(
	    ClickEvent e){
		  
		  LoginForm.this.hide();		  
	  }
}
